package com.interview.net;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpSend {

    public static void main(String[] args) {
        DatagramSocket ds = null;
        DatagramPacket dp = null;
        try {
            ds = new DatagramSocket();//发送数据
            String info = "hello world hello 中国";
            //System.out.println("字符串的长度：" + info.length());
            //System.out.println("字符串对应的字节数组长度：" + info.getBytes().length);
            dp = new DatagramPacket(info.getBytes(), //消息的字节数组
                                    info.getBytes().length,//字节数组的长度
                                    InetAddress.getByName("localhost"),//服务器的IP
                                3000); //服务器端口号
            //发送数据
            ds.send(dp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ds.close();
        }

    }

}